# -*-makefile-*-
# Makefile.in
# @configure_input@

# configure output
BDR_PGVERCOMPAT_INCDIR=@BDR_PGVERCOMPAT_INCDIR@
HAVE_SEQAM=@HAVE_SEQAM@
BDR_PG_MAJORVERSION=@BDR_PG_MAJORVERSION@
# end configure output

bdr_subdir = .
bdr_top_builddir = .

MODULE_big = bdr

EXTENSION = bdr

DATA = \
	extsql/bdr--1.0.0.0.sql \
	extsql/bdr--1.0.0.0--1.0.1.0.sql \
	extsql/bdr--1.0.1.0--1.0.2.0.sql \
	extsql/bdr--1.0.2.0--1.0.3.0.sql \
	extsql/bdr--1.0.3.0--2.0.0.0.sql \
	extsql/bdr--2.0.0.0--2.0.1.0.sql \
	extsql/bdr--2.0.1.0--2.0.2.0.sql \
	extsql/bdr--2.0.2.0--2.0.3.0.sql \
	extsql/bdr--2.0.3.0--2.0.4.0.sql \
	extsql/bdr--2.0.4.0--2.0.5.0.sql \
	extsql/bdr--2.0.5.0--2.0.6.0.sql

# Remember to add $(call genextfile,from,to) invocations for these
DATA_built = \
	extsql/bdr--1.0.1.0.sql \
	extsql/bdr--1.0.2.0.sql \
	extsql/bdr--1.0.3.0.sql \
	extsql/bdr--2.0.0.0.sql \
	extsql/bdr--2.0.1.0.sql \
	extsql/bdr--2.0.2.0.sql \
	extsql/bdr--2.0.3.0.sql \
	extsql/bdr--2.0.4.0.sql \
	extsql/bdr--2.0.5.0.sql \
	extsql/bdr--2.0.6.0.sql

DOCS = README.bdr bdr.conf.sample

# pgxs doesn't know how to handle directories of docs, or built docs, so the
# html docs will get separate rules below

# We can't use SCRIPTS_built because it isn't supported by 9.4 and we can't use
# SCRIPTS because it breaks VPATH builds.  So we'll just inject support later
# for SCRIPTS_built if PGXS doesn't support it.
SCRIPTS_built = \
	scripts/bdr_initial_load \
	bdr_init_copy \
	bdr_dump
all: \
	scripts/bdr_initial_load \
	bdr_init_copy \
	bdr_dump

# We can't use Makefile.global's MAJORVERSION here yet
ifneq ($(BDR_PG_MAJORVERSION),94)
SCRIPTS_built += bdr_resetxlog
all: bdr_resetxlog
endif

# When in development add -Werror
PG_CPPFLAGS = -std=gnu89 -I$(srcdir)/$(BDR_PGVERCOMPAT_INCDIR) -I$(libpq_srcdir) -Wall -Wmissing-prototypes -Wmissing-declarations $(EXTRA_CFLAGS)
SHLIB_LINK = $(libpq)

OBJS = \
	bdr.o \
	bdr_apply.o \
	bdr_dbcache.o \
	bdr_ddlrep.o \
	bdr_ddlrep_truncate.o \
	bdr_perdb.o \
	bdr_catalogs.o \
	bdr_conflict_handlers.o \
	bdr_conflict_logging.o \
	bdr_commandfilter.o \
	bdr_common.o \
	bdr_compat.o \
	bdr_count.o \
	bdr_executor.o \
	bdr_init_replica.o \
	bdr_label.o \
	bdr_locks.o \
	bdr_nodecache.o \
	bdr_messaging.o \
	bdr_monitoring.o \
	bdr_output.o \
	bdr_output_origin_filter.o \
	bdr_pgutils.o \
	bdr_protocol.o \
	bdr_relcache.o \
	bdr_remotecalls.o \
	bdr_seq2.o \
	bdr_shmem.o \
	bdr_supervisor.o \
	bdr_upgrade.o

ifdef RUN_FAILING_TESTS
# Empty if there aren't any expected-to-fail tests at the moment.
FAILING_TESTS :=
else
# By default we don't run failing tests, this will always be empty.
FAILING_TESTS :=
endif

ifeq ($(HAVE_SEQAM),1)
OBJS += bdr_seq.o bdr_ddlrep_deparse.o
REGRESS_COMPAT :=
DDLTESTS := \
	ddl_enable_ddl \
	ddl_create \
	ddl_alter_table \
	ddl_extension \
	ddl_function \
	ddl_grant \
	ddl_mixed \
	ddl_namespace \
	ddl_replication_set \
	ddl_sequence \
	ddl_view \
	ddl_disable_ddl

SEQTESTS := dml_sequence
else
OBJS += bdr_seq_stub.o
REGRESS_COMPAT := 
# Can't run ddl_sequence on 9.6
# DDL disabled on 9.6 for now
#DDLTESTS := \
#	ddl_enable_ddl \
#	ddl_create \
#	ddl_alter_table \
#	ddl_extension \
#	ddl_function \
#	ddl_grant \
#	ddl_mixed \
#	ddl_namespace \
#	ddl_replication_set \
#	ddl_view \
#	ddl_disable_ddl

SEQTESTS :=
endif

# DDL tests run via bdr.bdr_replicate_ddl_command
# wrapper.
DDLTESTS_FUNC := \
	ddl_fn/ddl_enable_ddl \
	ddl_fn/ddl_create \
	ddl_fn/ddl_alter_table \
	ddl_fn/ddl_extension \
	ddl_fn/ddl_function \
	ddl_fn/ddl_grant \
	ddl_fn/ddl_mixed \
	ddl_fn/ddl_namespace \
	ddl_fn/ddl_replication_set \
	ddl_fn/ddl_view \
	ddl_fn/ddl_disable_ddl


include Makefile.global

$(info Building against PostgreSQL $(MAJORVERSION))

DUMPOBJS = $(BDR_DUMP_DIR)/pg_dump.o \
	$(BDR_DUMP_DIR)/common.o \
	$(BDR_DUMP_DIR)/pg_dump_sort.o \
	$(BDR_DUMP_DIR)/pg_backup_archiver.o \
	$(BDR_DUMP_DIR)/pg_backup_db.o \
	$(BDR_DUMP_DIR)/pg_backup_custom.o \
	$(BDR_DUMP_DIR)/pg_backup_null.o \
	$(BDR_DUMP_DIR)/pg_backup_tar.o \
	$(BDR_DUMP_DIR)/pg_backup_directory.o \
	$(BDR_DUMP_DIR)/pg_backup_utils.o \
	$(BDR_DUMP_DIR)/parallel.o \
	$(BDR_DUMP_DIR)/compress_io.o \
	$(BDR_DUMP_DIR)/dumputils.o

BDR_DUMP_DIR := pg_dump$(BDR_PG_MAJORVERSION)
ifeq ($(MAJORVERSION),9.6)
BDR_DUMP_LIBS := -lpgfeutils
DUMPOBJS += $(BDR_DUMP_DIR)/keywords.o
else ifeq ($(MAJORVERSION),9.4)
BDR_DUMP_LIBS :=
DUMPOBJS += $(BDR_DUMP_DIR)/keywords.o \
	$(BDR_DUMP_DIR)/kwlookup.o
else
BDR_DUMP_LIBS := -lz -lpgfeutils
DUMPOBJS += $(BDR_DUMP_DIR)/string_utils.o
endif

ifneq ($(MAJORVERSION),9.4)
RESETXLOG_OBJS := pg_resetxlog/$(BDR_PG_MAJORVERSION)/pg_resetwal.o
RESETXLOG_LIBS := -lpgfeutils

bdr_resetxlog_dir:
	mkdir -p pg_resetxlog/$(BDR_PG_MAJORVERSION)

.PHONY: bdr_resetxlog_dir

bdr_resetxlog: bdr_resetxlog_dir $(RESETXLOG_OBJS)
	$(CC) $(CFLAGS) $(RESETXLOG_OBJS) $(libpq_pgport) $(LDFLAGS) $(LDFLAGS_EX) -o $@$(X)
else
RESETXLOG_OBJS := 
RESETXLOG_LIBS :=

bdr_resetxlog:
	@echo "bdr_resetxlog is not built on 9.4"
endif

# Ensure Makefiles are up2date (should we move this to Makefile.global?)
Makefile: Makefile.in config.status configure
	./config.status $@

Makefile.global: Makefile.global.in config.status configure
	./config.status $@

run_tests: run_tests.in  config.status configure
	./config.status $@

configure: configure.in
	cd ${bdr_abs_srcdir} && autoreconf

DATE=$(shell date "+%Y-%m-%d")
GITHASH=$(shell if [ -e .distgitrev ]; then cat .distgitrev; else GIT_DIR=${bdr_abs_srcdir}/.git git rev-parse --short HEAD; fi)

bdr_version.h: bdr_version.h.in
	sed '0,/BDR_VERSION_DATE/s,\(BDR_VERSION_DATE\).*,\1 "$(DATE)",;0,/BDR_VERSION_GITHASH/s,\(BDR_VERSION_GITHASH\).*,\1 "$(GITHASH)",' $< >$@

bdr.o: bdr_version.h

bdr_init_copy: bdr_init_copy.o bdr_common.o bdr_pgutils.o
	$(CC) $(CFLAGS) $^ $(LDFLAGS) $(LDFLAGS_EX) $(libpq_pgport) -o $@$(X)

scripts/bdr_initial_load: scripts/bdr_initial_load.in
	@mkdir -p $(shell dirname $@)
	sed -e "s/BDR_VERSION/$(BDR_VERSION)/" -e "s/PG_VERSION/$(VERSION)/" $< > $@
	@chmod +x $@

#----
# Generate flattened extension files.
#
# This isn't needed on PostgreSQL 10 as it's smart enough to figure out a
# creation path, but we still need it for 9.6 and 9.4bdr.
#
# Note that make 3.81 doesn't like define with =, so don't add one even though
# that's how the manual shows the syntax. See
# http://stackoverflow.com/q/13260396/398670 .
#---
define genextfile
extsql/bdr--$(2).sql: extsql/bdr--$(1).sql extsql/bdr--$(1)--$(2).sql
	mkdir -p extsql
	cat $$^ > $$@
endef

$(eval $(call genextfile,1.0.0.0,1.0.1.0))
$(eval $(call genextfile,1.0.1.0,1.0.2.0))
$(eval $(call genextfile,1.0.2.0,1.0.3.0))
$(eval $(call genextfile,1.0.3.0,2.0.0.0))
$(eval $(call genextfile,2.0.0.0,2.0.1.0))
$(eval $(call genextfile,2.0.1.0,2.0.2.0))
$(eval $(call genextfile,2.0.2.0,2.0.3.0))
$(eval $(call genextfile,2.0.3.0,2.0.4.0))
$(eval $(call genextfile,2.0.4.0,2.0.5.0))
$(eval $(call genextfile,2.0.5.0,2.0.6.0))

pg_dump_dir:
	mkdir -p $(BDR_DUMP_DIR)

bdr_dump: pg_dump_dir $(DUMPOBJS)
	$(CC) $(CFLAGS) $(DUMPOBJS) $(libpq_pgport) $(LDFLAGS) $(LDFLAGS_EX) $(BDR_DUMP_LIBS) -o $@$(X)

doc:
	$(MAKE) -C doc all

install-doc:
	$(MAKE) -C doc install

all: all-lib bdr_init_copy bdr_dump

world: all doc

install-world: install install-doc

clean: additional-clean

maintainer-clean: additional-maintainer-clean

additional-clean:
	rm -f bdr_init_copy$(X) bdr_init_copy.o
	rm -f bdr_version.h
	rm -f .distgitrev
	rm -rf tmp_check
	rm -rf scripts/bdr_initial_load
	rm -f bdr_dump$(X) $(DUMPOBJS)
	rm -f bdr_resetxlog$(X) $(RESETXLOG_OBJS)
	rm -f extsql/bdr--0.[89].0.[0-9].sql
	rm -rf doc/html
	$(MAKE) -C doc clean

additional-maintainer-clean: clean
	rm -f configure
	rm -f config.status config.log
	rm -f bdr_config.h.in Makefile
	rm -f run_tests
	@rm -rf autom4te.cache/
	$(MAKE) -C doc maintainer-clean

# Disabled because these tests require "wal_level=logical", which
# typical installcheck users do not have (e.g. buildfarm clients).
installcheck: ;

ifeq ($(BDR_PG_MAJORVERSION),9.4)
# We must emulate SCRIPTS_built for Pg 9.4 as PGXS doesn't support it
install: install-scripts
install-scripts:
	$(INSTALL_SCRIPT) $(SCRIPTS_built) '$(DESTDIR)$(bindir)/'
.PHONY: install-scripts
installdirs: installdirs-scripts
installdirs-scripts:
	$(MKDIR_P) '$(DESTDIR)$(bindir)'
.PHONY: installdirs-scripts
endif

check: regresscheck noisolationcheck prove_installcheck

REGRESSCHECKS= \
	init \
	preseed \
	init_bdr \
	status \
	guc \
	schema \
	$(REGRESS_COMPAT) \
	preseed_check \
	supervisordb \
	upgrade \
	identifier \
	schema_desync \
	replorigin_filter \
	pause \
	catalog \
	terminate \
	skipchanges \
	pgreplicationslots \
	seq2 \
	read_only \
	$(FAILING_TESTS) \
	ddl_lock \
	$(DDLTESTS) \
	$(DDLTESTS_FUNC) \
	dml_basic \
	dml_contrib \
	dml_delete_pk \
	dml_extended \
	dml_replicate_ddl_command \
	dml_missing_pk \
	dml_toasted \
	$(SEQTESTS) \
	part_bdr


ISOLATIONCHECKS=\
	isolation/init \
	isolation/ddlconflict \
	isolation/dmlconflict_ii \
	isolation/dmlconflict_uu \
	isolation/dmlconflict_ud \
	isolation/dmlconflict_dd \
	isolation/alter_table \
	isolation/basic_triple_node
#	this test demonstrates a divergent conflict, so deactivate for now
#	isolation/update_pk_change_conflict

# XXX: Add a check that these are installed
REQUIRED_EXTENSIONS="btree_gist"
REQUIRED_TEST_EXTENSIONS="pg_trgm cube hstore"

REGRESSCONFIG=bdr_regress_bdr.conf

regresscheck: all install
	[ -e pg_hba.conf ] || ln -s $(bdr_abs_srcdir)/pg_hba.conf .

	mkdir -p results/ddl results/ddl_fn results/dml

	./run_tests --config $(bdr_abs_srcdir)/$(REGRESSCONFIG) \
		--testbinary src/test/regress/pg_regress \
		$(REGRESSCHECKS)

noisolationcheck:
	@echo "Isolation tests are now skipped by default even on 9.4"
	@echo "Use 'make isolationcheck' to run them"

.PHONY: noisolationcheck


ifeq ($(MAJORVERSION),9.4)
isolationcheck: all install
	mkdir -p results/isolation

	./run_tests --config $(bdr_abs_srcdir)/bdr_isolationregress.conf \
		--testbinary src/test/isolation/pg_isolation_regress \
		--dbname node1,node2,node3 \
		$(ISOLATIONCHECKS)

prove_installcheck:
	@echo "No prove / TAP support in 9.4, ignoring prove tests"

else
# pg_isolation_regress doesn't get installed by 96, only by 94bdr
isolationcheck:
	@echo "Cannot run isolation tests except on 9.4bdr"

#
# To run prove, we need to install a couple of things that are not
# usually coped from the Pg source tree to the install tree, namely
# all of src/test/perl needs to be copied to lib/postgresql/pgxs/src/test/perl/PostgresNode.pm .
#
# This isn't done by Pg's install command, which doesn't know how to install them. So you'll have
# to copy them manually or patch your postgres tree to install them:
#
#   https://www.postgresql.org/message-id/CAMsr%2BYFXv%2B2qne6xJW7z_25mYBtktRX5rpkrgrb%2BDRgQ_FxgHQ@mail.gmail.com
#
$(pgxsdir)/src/test/perl/PostgresNode.pm:
	@[ -e $(pgxsdir)/src/test/perl/PostgresNode.pm ] || ( echo -e "----ERROR----\nCan't run prove_installcheck, copy src/test/perl/* to $(pgxsdir)/src/test/perl/ and retry\n-------------" && exit 1)

prove_installcheck: $(pgxsdir)/src/test/perl/PostgresNode.pm
	rm -rf $(CURDIR)/tmp_check/
	cd $(srcdir) && TESTDIR='$(CURDIR)' PATH="$(shell $(PG_CONFIG) --bindir):$$PATH" PGPORT='6$(DEF_PGPORT)' top_builddir='$(CURDIR)/$(top_builddir)' PG_REGRESS='$(pgxsdir)/src/test/regress/pg_regress' $(PROVE) $(PG_PROVE_FLAGS) $(PROVE_FLAGS) $(or $(PROVE_TESTS),t/*.pl)

.PHONY: prove_installcheck
endif

bdr_pgbench_check: bdr_pgbench_check.sh
	sed -e 's,@bindir@,$(bindir),g' \
	    -e 's,@libdir@,$(libdir),g' \
	    -e 's,@MAKE@,$(MAKE),g' \
	    -e 's,@top_srcdir@,$(top_srcdir),g' \
	  $< >$@
	chmod a+x $@

pgbenchcheck: bdr_pgbench_check
	./bdr_pgbench_check

distdir = bdr-$(BDR_VERSION)

git-dist: clean
	rm -f .distgitrev .distgittag
	if ! git diff-index --quiet HEAD; then echo >&2 "WARNING: git working tree has uncommitted changes to tracked files which were INCLUDED"; fi
	if [ -n "`git ls-files --exclude-standard --others`" ]; then echo >&2 "WARNING: git working tree has unstaged files which were IGNORED!"; fi
	echo $(GITHASH) > .distgitrev
	git name-rev --tags --name-only `cat .distgitrev` > .distgittag
	git ls-tree -r -t --full-tree HEAD --name-only |\
	  tar cjf "${distdir}.tar.bz2" --no-recursion --owner=root --group=root --mode=u=rwX,go=rX --transform="s|^|${distdir}/|" -T - \
	    .distgitrev .distgittag
	echo >&2 "Prepared ${distdir}.tar.bz2 for rev=`cat .distgitrev`, tag=`cat .distgittag`"
	rm -f .distgitrev .distgittag

PHONY: submake-regress

# if config.log has changed, force recompile
$(OBJS): .pg_version
.pg_version:
	@echo '$(MAJORVERSION)' | cmp -s - $@ || echo '$(MAJORVERSION)' > $@

export PG_CONFIG
export PATH

bdr_test_dummy_extension_all:
	mkdir -p bdr_test_dummy_extension
	$(MAKE) -C bdr_test_dummy_extension -f ${bdr_abs_srcdir}/bdr_test_dummy_extension/Makefile VPATH=${bdr_abs_srcdir}/bdr_test_dummy_extension all

bdr_test_dummy_extension_clean:
	$(MAKE) -C bdr_test_dummy_extension -f ${bdr_abs_srcdir}/bdr_test_dummy_extension/Makefile VPATH=${bdr_abs_srcdir}/bdr_test_dummy_extension clean

bdr_test_dummy_extension_install:
	mkdir -p bdr_test_dummy_extension
	$(MAKE) -C bdr_test_dummy_extension -f ${bdr_abs_srcdir}/bdr_test_dummy_extension/Makefile VPATH=${bdr_abs_srcdir}/bdr_test_dummy_extension install

all: bdr_test_dummy_extension_all
clean: bdr_test_dummy_extension_clean

# we do NOT install bdr_test_dummy_extension for the normal 'install' target,
# only before tests.
prove_installcheck: bdr_test_dummy_extension_install

.PHONY: all check regresscheck isolationcheck doc
